package com.gxsyh.nantie.statements.management.provider.service.impl;

import com.gxsyh.nantie.statements.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.statements.management.provider.mapper.TbDepartmentMapper;
import com.gxsyh.nantie.statements.management.provider.service.TbDepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TbDepartmentServiceImpl implements TbDepartmentService {

    @Resource
    private TbDepartmentMapper tbDepartmentMapper;

    @Override
    public List<TbDepartment> selectDepartmentAndSubordinate(Integer type,Long departmentId,Long parentId) {
        return tbDepartmentMapper.selectDepartmentAndSubordinate(type,departmentId,parentId);
    }

    @Override
    public List<TbDepartment> selectDepartmentSubordinate(Integer type, Long parentId) {
        List<TbDepartment> tbDepartments=tbDepartmentMapper.selectDepartment(type,parentId);
        if (tbDepartments==null){
            return null;
        }else{
            return tbDepartments;
        }
//        return tbDepartmentMapper.selectDepartment(type,parentId);
    }


    @Override
    public List<TbDepartment> selectAllWorkshopSubordinate(Integer type, Long parentId) {
        List<TbDepartment> tbDepartmentList=tbDepartmentMapper.selectDepartment(type,parentId);
        List<TbDepartment> tbDepartmentList1=new ArrayList<>();
        if (tbDepartmentList==null){
            return null;
        }else{
            for (TbDepartment list:tbDepartmentList)
            {
                List<TbDepartment> tbDepartments=selectDepartmentSubordinate(type,list.getId());
                if (tbDepartments==null)
                {
                    return tbDepartmentList;
                }
                for (TbDepartment subordinate:tbDepartments)
                {
                    tbDepartmentList1.add(subordinate);
                }
            }
            return tbDepartmentList1;
        }
    }

    @Override
    public String selectAllBelongToDepartment( Long parentId) {
        Long id=parentId;
        List<String> names=new ArrayList<>();
        String name="";
        for (int i=0;i<10;i++)
        {
            List<TbDepartment> tbDepartments=tbDepartmentMapper.selectBelongToDepartment(id);
            if(tbDepartments==null||tbDepartments.get(0)==null)
            {
               return "管理员";
            }
            id=tbDepartments.get(0).getParentId();
            names.add(tbDepartments.get(0).getName());
            if (tbDepartments.get(0).getParentId()==0)
            {
                return "管理员";
            }
            if (tbDepartments.get(0).getParentId()==1)
            {
                return name;
            }
        }

        for (int i=names.size()-1;i>=0;i--)
        {
            name+=names.get(i);
        }
        return name;
    }

}
